package com.bazhua.bazo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bazhua.bazo.repository.entity.*;
import com.bazhua.bazo.repository.mapper.AgentRecordMapper;
import com.bazhua.bazo.repository.mapper.CouponCheckRecordMapper;
import com.bazhua.bazo.repository.model.AgentRecordModel;
import com.bazhua.bazo.repository.model.CouponCheckModel;
import com.bazhua.bazo.repository.model.ShopStatus;
import com.bazhua.bazo.repository.model.UserType;
import com.bazhua.bazo.repository.model.result.CommonResult;
import com.bazhua.bazo.repository.model.result.PageListResult;
import com.bazhua.bazo.repository.model.result.ResultUtils;
import com.bazhua.bazo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: starmile
 * @Description:
 * @Create: 2021-03-02 0:12
 **/
@Service
public class AgentRecordServiceImpl extends ServiceImpl<AgentRecordMapper, AgentRecord> implements IAgentRecordService {

    private final IWechatUserService wechatUserService;

    private final IShopInfoService shopInfoService;

    public AgentRecordServiceImpl(IShopInfoService shopInfoService, IWechatUserService wechatUserService) {
        this.shopInfoService = shopInfoService;
        this.wechatUserService = wechatUserService;
    }

    private ShopInfo getShop(Long shopId) {
        QueryWrapper<ShopInfo> shopInfoQueryWrapper = new QueryWrapper<>();
        shopInfoQueryWrapper.eq("id", shopId);
        ShopInfo shop = shopInfoService.getOne(shopInfoQueryWrapper);
        return shop;
    }

    public CommonResult add(Long shopId) {
        ShopInfo shopInfo = getShop(shopId);
        QueryWrapper<AgentRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_id", shopId);
        AgentRecord agentRecord = baseMapper.selectOne(wrapper);
        if (agentRecord == null){
            agentRecord = new AgentRecord();
            agentRecord.setShopId(shopId);
            agentRecord.setUserId(shopInfo.getShopExtensionUser());
            agentRecord.setAudit(true);
            agentRecord.setCreateTime(new Date());
            baseMapper.insert(agentRecord);
        } else {
            agentRecord.setAudit(shopInfo.getShopStatus() == ShopStatus.Approved);
            baseMapper.updateById(agentRecord);
        }
        return ResultUtils.ok();
    }

    public PageListResult getPage(Integer pageNum, Integer pageSize, Long userId) {
        QueryWrapper<AgentRecord> queryWrapper = new QueryWrapper();
        WechatUser wechatUser = wechatUserService.findById(userId);
        queryWrapper.eq("audit", true);
        if (wechatUser != null && wechatUser.getUserType() == UserType.Salesman) {
            queryWrapper.eq("user_id", wechatUser.getId());
        }
        Page<AgentRecord> page = new Page<>(pageNum, pageSize);
        page = baseMapper.selectPage(page, queryWrapper);
        if (page.getRecords() != null && page.getRecords().size() > 0) {
            Page<AgentRecordModel> modelPage = new Page<>();
            modelPage.setCurrent(page.getCurrent());
            modelPage.setSize(page.getSize());
            modelPage.setTotal(page.getTotal());
            modelPage.setPages(page.getPages());
            modelPage.setMaxLimit(page.getMaxLimit());
            List<AgentRecordModel> list = new ArrayList<>();
            for (AgentRecord agentRecord : page.getRecords()) {
                AgentRecordModel agentRecordModel = new AgentRecordModel();
                WechatUser user = wechatUserService.findById(agentRecord.getUserId());
                if (user == null)
                    continue;
                ShopInfo shopInfo = getShop(agentRecord.getShopId());
                if (shopInfo == null)
                    continue;
                agentRecordModel.setUserName(user.getUserName());
                agentRecordModel.setCreateTime(agentRecord.getCreateTime());
                agentRecordModel.setId(agentRecord.getId());
                agentRecordModel.setShopName(shopInfo.getShopName());
                list.add(agentRecordModel);
            }
            modelPage.setRecords(list);
            return ResultUtils.pageOk(modelPage);
        }
        return ResultUtils.pageError(0, "查询数据为空");
    }
}
